import { CheckUtil } from '/@/lib/check';

/** 枚举相关包 **/
export const EnumUtil = {
  /**
   * {ENUM_NAME: [值，含义，样式]}，如需增加需要更改枚举定义函数
   * @param def
   * @returns
   * @example
   * ```
   *    const status = createEnum({
   *      AUDITING: [10, '审核中', 'color:red', {type: 1}],
   *      PASS: [20, '审核通过', 'color:blue'],
   *    });
   * ```
   */
  create(def) {
    const keyToLabelMap = Object.create(null),
      keyToValueMap = Object.create(null),
      keyToStyleMap = Object.create(null),
      keyToPropsMap = Object.create(null),
      valueToLabelMap = Object.create(null),
      valueToStyleMap = Object.create(null),
      valueToPropsMap = Object.create(null),
      valueLabelList = [];

    Object.keys(def).forEach((key) => {
      const [value, label, style = '', params = {}] = def[key];
      const props = { ...params, key, value, label, style, html: `<span style="${style || ''}">${label || ''}</span>` };

      if (CheckUtil.isNotEmpty(key)) {
        keyToLabelMap[key] = label;
        keyToValueMap[key] = value;
        keyToStyleMap[key] = style;
        keyToPropsMap[key] = props;
        valueLabelList.push(props);
      }

      if (CheckUtil.isNotEmpty(value)) {
        valueToLabelMap[value] = label;
        valueToStyleMap[value] = style;
        valueToPropsMap[value] = props;
      }
    });

    return {
      ...keyToPropsMap,

      // 获取部分枚举，[startValue, endValue]
      getValueLabelListRange(startKey, endKey, includeStart = true, includeEnd = true) {
        const sortValueList = valueLabelList.map((o) => o.value).sort((a, b) => a - b);
        let startValue = sortValueList[0];
        let endValue = sortValueList[sortValueList.length - 1];
        if (startKey && CheckUtil.isNotEmpty(keyToValueMap[startKey])) {
          startValue = keyToValueMap[startKey];
        }
        if (endKey && CheckUtil.isNotEmpty(keyToValueMap[endKey])) {
          endValue = keyToValueMap[endKey];
        }

        return valueLabelList.filter(
          (o) =>
            (o.value > startValue || (includeStart ? o.value === startValue : false)) &&
            (o.value < endValue || (includeEnd ? o.value === endValue : false)),
        );
      },

      getValueStringRange(...params) {
        return this.getValueLabelListRange(...params)
          .map((o) => o.value)
          .join(',');
      },

      // 获取部分枚举
      getValueLabelList(...keyList) {
        if (CheckUtil.isNotEmptyArray(keyList)) {
          return keyList.length > 0 ? valueLabelList.filter((o) => keyList.includes(o.key)) : valueLabelList;
        }

        return valueLabelList;
      },

      getLabelByKey(key) {
        return keyToLabelMap[key] || '';
      },

      getLabelByValue(value) {
        return valueToLabelMap[value] || '';
      },

      getValueByKey(key) {
        return keyToValueMap[key] || '';
      },

      getStyleByKey(key) {
        return keyToStyleMap[key] || '';
      },

      getStyleByValue(value) {
        return valueToStyleMap[value] || '';
      },

      getPropsByKey(key) {
        return keyToPropsMap[key] || Object.create(null);
      },

      getPropsByValue(value) {
        return valueToPropsMap[value] || Object.create(null);
      },

      getHtmlByKey(key) {
        if (CheckUtil.isEmpty(key)) {
          return '';
        }

        return `<span style="${keyToStyleMap[key] || ''}">${keyToLabelMap[key] || ''}</span>`;
      },

      getHtmlByValue(value) {
        if (CheckUtil.isEmpty(value)) {
          return '';
        }

        return `<span style="${valueToStyleMap[value] || ''}">${valueToLabelMap[value] || ''}</span>`;
      },
    };
  },
};

/** 枚举相关包 **/
export const MapUtil = {
  create(def) {
    const valueToKeyMap = Object.create(null),
      keyValueList = [];

    Object.keys(def).forEach((key) => {
      const value = def[key];

      if (CheckUtil.isNotEmpty(key)) {
        keyValueList.push({ key, value });
      }

      if (CheckUtil.isNotEmpty(value)) {
        valueToKeyMap[value] = key;
      }
    });

    return {
      ...def,

      // 获取部分枚举
      getKeyValueList(...keyList) {
        if (CheckUtil.isNotEmptyArray(keyList)) {
          return keyList.length > 0 ? keyValueList.filter((o) => keyList.includes(o.key)) : keyValueList;
        }

        return keyValueList;
      },

      getKeyByValue(value) {
        return valueToKeyMap[value] || '';
      },
    };
  },
};
